package com.delivery.activity;


import org.json.JSONException;
import org.json.JSONObject;

import com.delivery.common.BaseActivity;
import com.delivery.module.ConfigModule;
import com.delivery.service.ICategoriesContent;
import com.delivery.service.impl.CategoriesContentService;
import com.delivery.utils.StringUtils;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public final class ContactActivity extends BaseActivity {
	
	private ICategoriesContent categoriesContentService = new CategoriesContentService();
	 
	public static MyHandler myHandler;
	
	private static StringBuffer params = new StringBuffer();
	
	private String TAG = "ContactActivity";
	
	private static TextView tvName,tvCompanyName,tvFax,tvMobile,tvEmail,tvCompanyTime;
	
	private static String json = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact);
		tvName = (TextView) findViewById(R.id.name);
		tvCompanyName = (TextView) findViewById(R.id.company);
		tvCompanyTime = (TextView) findViewById(R.id.companyTime);
		tvEmail = (TextView) findViewById(R.id.email);
		tvFax = (TextView) findViewById(R.id.fax);
		tvMobile = (TextView) findViewById(R.id.mobileTelphone);
		myHandler = new MyHandler(this,1);
		MyThread my = new MyThread();
		new Thread(my).start();
	}

	// 更新后台数据
	class MyThread implements Runnable {
		public void run() {
			try {
				Thread.sleep(REST_TIME);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			String msglist = "1";
			Message msg = new Message();
			Bundle b = new Bundle();// 存放数据
			b.putString("rmsg", msglist);
			msg.setData(b);
			myHandler.sendMessage(msg); // 向Handler发送消息,更新UI

		}
	}

	public class MyHandler extends Handler {
		private ContactActivity mContext;
		public MyHandler(Context conn,int a) {
			mContext = (ContactActivity)conn;
		}

		public MyHandler(Looper L) {
			super(L);
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bundle b = msg.getData();
			String rmsg = b.getString("rmsg");
			if ("1".equals(rmsg) && StringUtils.isEmpty(json)) {
				try{
					json = getCategoriesContentJson();
					if(StringUtils.isEmpty(json)){
						return;
					}
					
					JSONObject jsonObject = new  JSONObject(json);
					if(jsonObject==null){
						return;
					}
					if(StringUtils.isNotEmpty(jsonObject.getString("company"))){
						tvCompanyName.setText("公司的名称："+jsonObject.getString("company"));
					}
					if(StringUtils.isNotEmpty(jsonObject.getString("name"))){
						tvName.setText("联系人名字："+jsonObject.getString("name"));
					}
					if(StringUtils.isNotEmpty(jsonObject.getString("mobileTelphone"))){
						tvMobile.setText("联系人电话："+jsonObject.getString("mobileTelphone"));
					}
					if(StringUtils.isNotEmpty(jsonObject.getString("company"))){
						tvEmail.setText("联系人邮箱："+jsonObject.getString("email"));
					}
					if(StringUtils.isNotEmpty(jsonObject.getString("fax"))){
						tvFax.setText("传真："+jsonObject.getString("fax"));
					}
					if(StringUtils.isNotEmpty(jsonObject.getString("companyTime"))){
						tvCompanyTime.setText("公司成立时间："+jsonObject.getString("companyTime"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}
		
		/**
		 * 获取远程服务器数据，返回格式是json
		 * @return
		 */
		public String getCategoriesContentJson(){
			params.delete(0, params.length());
			params.append(ConfigModule.GET_CONTACT_URL);
			System.out.println(params);
		     try {
		    	 return  this.categoriesContentService.getCategoriesContents(String.valueOf(params));
			 } catch (Exception e) {
				e.printStackTrace();
				Log.e(TAG, "query categoriesContentService is fail.");
				return "";
			 }
		}
	
}
